﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Diploma.Models;
using DiplomaWeb.Models;
using DiplomaWeb.Models.Account;
using DiplomaWeb.Models.Developer;

namespace DiplomaWeb.Controllers
{
    [Authorize(Roles = "Developer")]
    public class DeveloperController : MasterController
    {
        public DeveloperController(DataManager dataManager)
            : base(dataManager) { }

        public ActionResult Home()
        {
            var viewModel = new ChangeUserViewMode();
            viewModel.ToViewModel(DataManager.UsersRepository.GetUsers().OrderBy(r => r.Surname));
            return View(viewModel);
        }

        public ActionResult UserProfile(ChangeUserViewMode model)
        {
            FormsService.SignOut();
            FormsService.SignIn(model.SelectedUser);
            return RedirectToRoute("Default", new { controller = "Account", action = "ToPage" });
        }

        public ActionResult Logs(LogViewModel model)
        {
            switch (model.SelectedType)
            {
                case "Error":
                    model.ToViewModel(DataManager.LoggerRepository.GetErrors());
                    break;
                case "Debug":
                    model.ToViewModel(DataManager.LoggerRepository.GetDebugs());
                    break;
                case "Info":
                    model.ToViewModel(DataManager.LoggerRepository.GetInfos());
                    break;
                case "SignIn":
                    model.ToViewModel(DataManager.LoggerRepository.GetLogging());
                    break;
                default:
                    model.ToViewModel(DataManager.LoggerRepository.GetAll());
                    break;
            }
            return View(model);
        }

        public ActionResult ChangePassword()
        {
            return View(new ChangePasswordDeveloperViewModel());
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordDeveloperViewModel model)
        {
            DataManager.UsersRepository.UpdatePassword(model.SelectedUserId, model.NewPassword);
            model.IsSaved = true;
            return View(model);
        }
    }
}
